<template>
    <div class="w750" :class="themes.theme" :style="{ '--themescolor': themes.color }">
        <div class="invoice">
            <div class="mt-1 rounded-15 overflow-h">
                <van-cell-group v-if="params.titleInfo">
                    <van-cell title="发票类型" :value="params.invoiceTypeVal" value-class="font-13" :border="false" />
                    <van-cell title="抬头类型" :value="params.titleInfo.invTitleType=='PERSONAL'?'个人或事业单位':'企业'" value-class="font-13" :border="false" />
                    <van-cell title="发票抬头" :value="params.titleInfo.invTitle" value-class="font-13" :border="false" />
                    <van-cell title="发票内容" value="明细" value-class="font-13" :border="false" />
                    <van-cell
                        v-if="params.titleInfo.invTitleType == 'ENTERPRISE'"
                        title="税号"
                        :value="params.titleInfo.invIdfctMark"
                        value-class="font-13"
                        :border="false"
                    />
                    <van-cell
                        title="开户银行"
                        v-if="params.titleInfo.invDepositBank"
                        :value="params.titleInfo.invDepositBank"
                        value-class="font-13"
                        :border="false"
                    />
                    <van-cell
                        title="银行账号"
                        v-if="params.titleInfo.invBankAccout"
                        :value="params.titleInfo.invBankAccout"
                        value-class="font-13"
                        :border="false"
                    />
                    <van-cell
                        title="企业地址"
                        v-if="params.titleInfo.invRegAddr"
                        :value="params.titleInfo.invRegAddr"
                        value-class="font-13"
                        :border="false"
                    />
                    <van-cell
                        title="企业电话"
                        v-if="params.titleInfo.invRegPhone"
                        :value="params.titleInfo.invRegPhone"
                        value-class="font-13"
                        :border="false"
                    />
                    <template v-if="params.invoiceType == 'ORDINARY_INVOICE'">
                        <van-cell title="收票人手机号" :value="params.invoiceMoblie" value-class="font-13" :border="false" />
                        <van-cell title="收票人邮箱" :value="params.invoiceEmail" value-class="font-13" :border="false" />
                    </template>
                    <template v-else>
                        <van-cell title="发票收货人" :value="params.invoiceInfo.invConsignee" value-class="font-13" :border="false" />
                        <van-cell title="收票人手机号" :value="params.invoiceInfo.invPhone" value-class="font-13" :border="false" />
                        <van-cell title="发票收货地址" :value="params.invoiceInfo.invAddress" value-class="font-13" :border="false" />
                    </template>
                    <!-- 当下单后申请才显示 -->
                    <template v-if="type == 1">
                        <van-cell title="开票金额" :value="params.invoiceInfo.invoiceAmount" value-class="font-13" :border="false" />
                    </template>
                </van-cell-group>
                <div style="margin: 16px; margin-top: 60px; padding-bottom: 30px">
                    <van-button
                        @click="onSubmit"
                        round
                        block
                        type="info"
                        native-type="submit"
                        :color="themes.color"
                        style="height: 40px; font-size: 16px"
                    >
                        确认提交
                    </van-button>
                    <van-button
                        @click="goback"
                        round
                        block
                        type="info"
                        native-type="submit"
                        class="mt-1"
                        color="#fff"
                        style="height: 40px; font-size: 16px; margin-top: 14px"
                    >
                        <span class="text-main">返回修改</span>
                    </van-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { invoice, updateInvoice } from 'api/invoiceManage'

export default {
    data() {
        return {
            type: 0, //0为付款前 1为付款后申请发票
            params: {} //参数
        }
    },

    watch: {},
    mounted() {
        this.params = JSON.parse(sessionStorage.getItem('invoice'))
        this.type = this.$route.query.type
    },
    methods: {
        // 提交
        onSubmit() {
            this.$dialog
                .confirm({
                    message: '是否确认申请开票'
                })
                .then(() => {
                    // 先保存抬头 因为用户可能会修改抬头内容
                    // 如果id为真 则证明是修改抬头
                    if (this.params.titleInfo.id) {
                        // 删除不需要的东西
                        delete this.params.titleInfo.createTime
                        delete this.params.titleInfo.shopId
                        delete this.params.titleInfo.updateTime
                        delete this.params.titleInfo.userId
                    }
										this.params.titleInfo.subNumber = this.params.invoiceInfo.subNumber
                    //保存抬头请求
                    invoice.saveUserInvoiceTitle(this.params.titleInfo).then((res) => {
                        if (res.status !== 1) {
                            this.$toast(res.msg)
                            return
                        }
                        // 如果是下单后开票
                        if (this.type == 1) {
                            let params = {}
                            params.businessFlowAgg = this.$route.query.subNumber //订单号
                            // 如果是电子票
                            if (this.params.invoiceType == 'ORDINARY_INVOICE') {
                                params.invModality = 'ELECTRONICS_INV' //发票形式 写死
                                params.invEmail = this.params.invoiceEmail //收票电子邮箱
                                params.invPhone = this.params.invoiceMoblie //收票人手机号
                            } else {
                                // 纸质票
                                params.invModality = 'PAPER_INV' //发票形式 写死
                                params.invAddress = this.params.invoiceInfo.invAddress //收货地址
                                params.invConsignee = this.params.invoiceInfo.invConsignee //	收货人
                                params.invPhone = this.params.invoiceInfo.invPhone //收票人手机号
                            }
                            params.invBankAccout = this.params.titleInfo.invBankAccout //银行账号
                            params.invDepositBank = this.params.titleInfo.invDepositBank //	开户银行
                            params.invIdfctMark = this.params.titleInfo.invIdfctMark //	纳税人识别码
                            params.invRegAddr = this.params.titleInfo.invRegAddr //	注册地址
                            params.invRegPhone = this.params.titleInfo.invIdfctMark //	注册电话
                            params.invTitle = this.params.titleInfo.invTitle //发票抬头
                            params.invTitleType = this.params.titleInfo.invTitleType //发票抬头类型
                            params.invTotalAmount = this.params.invoiceInfo.invoiceAmount //发票总金额
                            params.invType = this.params.invoiceType //发票类型
                            // 如果是修改发票
                            if (this.$route.query.id) {
                                params.id = this.$route.query.id //发票id
                                invoice.updateInvoice(params).then((result) => {
                                    if (result.status !== 1) {
                                        this.$toast(result.msg)
                                        return
                                    }
                                    this.$toast('申请成功')
                                    this.$router.go(-2)
                                })
                            } else {
                                invoice.applyInvoice(params).then((result) => {
                                    if (result.status !== 1) {
                                        this.$toast(result.msg)
                                        return
                                    }
                                    this.$toast('申请成功')
                                    this.$router.go(-2)
                                })
                            }
                        } else {
                            // 下单前开票
                            sessionStorage.setItem('invoiceInfo', JSON.stringify(this.params))
                            this.$router.go(-2)
                        }
                        sessionStorage.removeItem('invoice')
                    })
                })
        },
        //返回
        goback() {
            this.$router.back()
        },

        //填写完成  保存
        saveInvoice() {}
    },
    computed: {
        ...mapState(['themes'])
    }
}
</script>
<style scoped></style>
